package com.autoagent.ai_live_master.webSocket.listener;

import com.autoagent.ai_live_master.common.event.MessageEvent;
import com.autoagent.ai_live_master.webSocket.handler.SocketIOHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 消息事件监听器
 * 监听消息事件并广播给前端
 */
@Slf4j
@Component
public class MessageEventListener {

    @Autowired
    private SocketIOHandler socketIOHandler;

    /**
     * 监听消息事件
     */
    @EventListener
    public void handleMessageEvent(MessageEvent event) {
        try {
            log.debug("收到消息事件: type={}, content={}", event.getType(), event.getContent());
            socketIOHandler.broadcast(event.getRoomId(), event.getContent());
        } catch (Exception e) {
            log.error("处理消息事件失败: {}", e.getMessage(), e);
        }
    }
}
